<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Models\Admin\AdminListField;
use Illuminate\Http\Request;

class ListFieldController extends AdminController
{
    use AdminTrait;
    protected $model;

    public function __construct(AdminListField $model)
    {
        $this->model = $model;
    }

    public function saveField(Request $request)
    {
        $type = $request->type;
        $info = $this->model->where('type',$type)->where('is_default',0)->first();
        if (!empty($info)){
            $info->fields = $request->fields;
            $flg = $info->save();
        }else{
            $data = [
                'fields' => $request->fields,
                'type' => $type,
                'is_default'=>0,
            ];
            $flg = $this->model->insert($data);
        }
        if ($flg){
            return $this->success();
        }else{
            return $this->error(['40007','设置失败']);
        }
    }

}
